/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.server.ds;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.bioteam.appweb.ds.DSPersister;
import net.bioteam.appweb.ds.DSServer;
import net.bioteam.appweb.ds.SrcDataset;
import net.bioteam.appweb.ds.Subscription;
import net.bioteam.appweb.gwt.apps.clientandserver.ServerException;
import net.bioteam.appweb.gwt.apps.clientandserver.ds.DSProxy;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DSProxyImpl extends RemoteServiceServlet implements DSProxy
{
	public static final String NS = "http://ids.bioteam.net/BiPod/DBNet/Server";

	private List<SrcDataset> loadDatasets() throws ServerException
	{
		List<SrcDataset> dss = (List<SrcDataset>) getThreadLocalRequest().getSession().getAttribute("dss");
		if (dss != null)
		{
			return dss;
		}

		try
		{
			dss = DSServer.loadDatasetsForSubscription();
		} catch (Exception e)
		{
			throw new ServerException(e.getMessage());
		}
		getThreadLocalRequest().getSession().setAttribute("dss", dss);
		return dss;

	}

	public SrcDataset[] getDatasets() throws ServerException
	{

		List<SrcDataset> dss=loadDatasets();
		SrcDataset ret[]=new SrcDataset[dss.size()];
		Map<String, Subscription> subs;
		try
		{
			subs = DSPersister.loadSubscription();
		} catch (Exception e)
		{
			throw new ServerException(e.getMessage());
		}
		for (int i=0; i<dss.size();i++)
		{
			SrcDataset src= dss.get(i);
			if (subs.containsKey(src.getName()))
				src.setSubscribed(true);
			else
				src.setSubscribed(false);
			ret[i]=src;
		}
		return ret;
	}

	public String Subscribe(List<String> names) throws ServerException
	{
		List<SrcDataset> dss = (List<SrcDataset>) getThreadLocalRequest().getSession().getAttribute("dss");
		if (dss == null)
		{
			try
			{
				dss = loadDatasets();
			} catch (Exception e)
			{
				throw new ServerException(e.getMessage());
			}
			getThreadLocalRequest().getSession().setAttribute("dss", dss);
		}

		List<SrcDataset> selected = new ArrayList<SrcDataset>(names.size());
		try
		{
			for (SrcDataset ds : dss)
			{

				if (names.contains(ds.getName()))
				{
					selected.add(ds);
				}

			}
			DSPersister.reSub(selected);
		} catch (Exception e)
		{
			throw new ServerException(e.getMessage());
		}
		return "";
	}

	public String enableDisable(List<String> dss) throws ServerException
	{

		try
		{
			Map<String, Subscription> subs = DSPersister.loadSubscription();
			Set<String> keys = new HashSet<String>();
			keys.addAll(subs.keySet());
			keys.removeAll(dss);

			for (String s : dss)
			{
				Subscription obj = subs.get(s);
				if (obj != null)
				{
					obj.setEnabled(true);
				}
			}
			for (String s : keys)
			{
				Subscription obj = subs.get(s);
				if (obj != null)
				{
					obj.setEnabled(false);
				}
			}
			DSPersister.writeSubscription(subs);
		} catch (Exception e)
		{
			throw new ServerException(e.getMessage());
		}
		return "";
	}

	public Subscription[] getSubscribedDatasets() throws ServerException
	{

		try
		{
			Map<String, Subscription> subs = DSPersister.loadSubscription();
			
			List<Subscription> dss = new ArrayList<Subscription>(subs.size());
			dss.addAll(subs.values());
			
			Collections.sort(dss, new Comparator<Subscription>()
			{

				public int compare(Subscription o1, Subscription o2)
				{
					return o1.getName().compareTo(o2.getName());
				}
			});

			return dss.toArray(new Subscription[0]);

		} catch (Exception e)
		{
			throw new ServerException(e.getMessage());
		}

	}
}
